<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商家名称" prop="people">
        <el-input
          v-model="queryParams.people"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="拼团标题" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入拼团标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      审核状态  下拉框选择-->
       <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in statusOptions"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="groupaList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="序号" align="center">-->
<!--        <template slot-scope="scope">-->
<!--          {{ scope.$index + 1 }} &lt;!&ndash; 使用 $index 获取当前行的索引，并加1以从1开始计数 &ndash;&gt;-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="拼团ID" align="center" prop="id" />
      <el-table-column label="拼团标题" align="center" prop="name" >
        <template slot-scope="scope">
          <span style="cursor: pointer;color: #00afff" @click="handleUpdate(scope.row)" >{{scope.row.name}}</span>
        </template>
      </el-table-column>
      <el-table-column label="几人团" align="center" prop="people" >
        <template slot-scope="scope">
          <span v-if="scope.row.people == 1">五人团</span>
          <span v-if="scope.row.people == 0">三人团</span>
          <span v-if="scope.row.people == 2">十人团</span>
          <span v-if="scope.row.people == 3">二十人团</span>
        </template>
      </el-table-column>
      <el-table-column label="每人限购" align="center" prop="limitBuy" />
      <el-table-column label="已成多少团" align="center" prop="totalGroup">
        <template slot-scope="scope">
          {{scope.row.totalGroup}} 人团
        </template>
      </el-table-column>
      <el-table-column label="提货时间" align="center" prop="pickUpTime" #default="SPick">
        <span>
          {{SPick.row.pickUpTime}}小时
        </span>
      </el-table-column>
      <el-table-column label="零售价格" align="center" prop="price" />
      <el-table-column label="拼团价格" align="center" prop="groupPrice" />
      <el-table-column label="所属店铺" align="center" prop="storeName" >
        <template slot-scope="scope">
          <span style="cursor: pointer;color: #00afff" @click="store(scope.row)" >{{scope.row.storeName}}</span>
        </template>
      </el-table-column>
<!--    0审核通过、1进行中,2已结束,3已停止，4内容违规，5等待开始   -->
      <el-table-column label="审核状态" align="center" prop="status" >
<!--        使用if判断来根据数据转换文字-->
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0" type="success">审核通过</el-tag>
          <el-tag v-if="scope.row.status == 1" type="primary">进行中</el-tag>
          <el-tag v-if="scope.row.status == 2" type="warning">已结束</el-tag>
          <el-tag v-if="scope.row.status == 3" type="danger">已停止</el-tag>
          <el-tag v-if="scope.row.status == 4" type="warning">内容违规</el-tag>
          <el-tag v-if="scope.row.status == 5" type="warning">等待开始</el-tag>
          <el-tag v-if="scope.row.status == 6" type="warning">审核不通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

<!--      <el-table-column label="封面图" align="center" prop="surfacePlot" >-->
<!--        <template slot-scope="scope">-->
<!--          <img :src="scope.row.surfacePlot" alt="" style="width: 100px;height: 100px">-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="配图1" align="center" prop="firstImg" >-->
<!--        <template slot-scope="scope">-->
<!--          <img :src="scope.row.firstImg" alt="" style="width: 100px;height: 100px">-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="配图2" align="center" prop="secondImg" >-->
<!--        <template slot-scope="scope">-->
<!--          <img :src="scope.row.secondImg" alt="" style="width: 100px;height: 100px">-->
<!--        </template>-->
<!--      </el-table-column>-->


<!--      <el-table-column label="商品说明" align="center" prop="remark" />-->
<!--      <el-table-column label="邀请码" align="center" prop="invitationCode" />-->
<!--      <el-table-column label="拼团标题" align="center" prop="name" />-->
      <el-table-column label="总团数量" align="center" prop="maxGroup" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="updatea(scope.row.id)"
            v-hasPermi="['system:groupa:edit']"
          >通过</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="updateab(scope.row.id)"
            v-hasPermi="['system:groupa:edit']"
          >不通过</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :total="total"
        @size-change="handleSizeChangea"
        @current-change="handleCurrentChangea"
      />
    </div>

<!--    <el-dialog v-model="centerDialogVisible" title="Warning" width="500" center>-->

<!--      <el-form ref="form" :model="dlog" :rules="rules" label-width="80px">-->
<!--      <el-form-item label="封面图" prop="surfacePlot">-->
<!--        <el-input v-model="dlog.surfacePlot" placeholder="请输入封面图" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="配图1" prop="firstImg">-->
<!--        <el-input v-model="dlog.firstImg" placeholder="请输入配图1" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="配图2" prop="secondImg">-->
<!--        <el-input v-model="dlog.secondImg" placeholder="请输入配图2" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="商品说明" prop="remark">-->
<!--        <el-input v-model="dlog.remark" placeholder="请输入商品说明" />-->
<!--      </el-form-item>-->
<!--      </el-form>-->

<!--    </el-dialog>-->

    <!-- 添加或修改拼团审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        图片展示封面图 使用img标签-->
        <template>
          <img :src="form.surfacePlot" alt="" style="width: 200px;height: 150px">
        </template>
        <template>
          <img :src="form.firstImg" alt="" style="width: 200px;height: 150px"></img>
        </template>
        <template>
          <img :src="form.secondImg" alt="" style="width: 200px;height: 150px"></img>
        </template>
        <el-form-item label="商品说明" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入商品说明" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listGroupa, getGroupa, delGroupa, addGroupa, updateGroupa, updateGroupab} from "@/api/system/groupa";

export default {
  name: "Groupa",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 标题弹框
      centerDialogVisible:false,
      // 存储图片弹框
      dlog:{
        remark:null,
        secondImg:null,
        firstImg:null,
        surfacePlot:null
      },
      // 总条数
      total: 0,
      // 拼团审核表格数据
      groupaList: [],
      // 弹出层标题
      title: "",
      // 审核状态
      statusOptions:[
      //   0审核通过、1进行中,2已结束,3已停止，4内容违规，5等待开始 6审核不通过
        {
          label: "审核通过",
          value: 0
        },
        {
          label: "进行中",
          value: 1
        },
        {
          label: "已结束",
          value: 2
        },
        {
          label: "已停止",
          value: 3
        },
        {
          label: "内容违规",
          value: 4
        },
        {
          label: "等待开始",
          value: 5
        },
        {
          label: "审核不通过",
          value: 6
        }
      ],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        people: null,
        totalGroup: null,
        pickUpTime: null,
        price: null,
        groupPrice: null,
        status: null,
        limitBuy: null,
        endTime: null,
        surfacePlot: null,
        firstImg: null,
        secondImg: null,
        invitationCode: null,
        name: null,
        maxGroup: null,
        storeName:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询拼团审核列表 */
    getList() {
      if(this.queryParams.id==null){
        this.loading = true;
        listGroupa(this.queryParams).then(response => {
          this.groupaList = response.data.list;
          this.total = response.data.total;
          this.loading = false;
        });
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 点击商铺根据商铺姓名进行查询
    store(row){
      this.$router.push({
        name: 'StoreMange',
        params: {
          storeName: row.storeName,
        }
      })
    },
    handleSizeChangea(val) {
      this.queryParams.pageSize = val;
      this.getList(); // 假设这是你的数据获取函数
    },
    handleCurrentChangea(val) {
      this.queryParams.pageNum = val;
      this.getList(); // 同样地，假设这是你的数据获取函数
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        people: null,
        totalGroup: null,
        pickUpTime: null,
        price: null,
        groupPrice: null,
        status: null,
        createTime: null,
        limitBuy: null,
        endTime: null,
        surfacePlot: null,
        firstImg: null,
        secondImg: null,
        remark: null,
        invitationCode: null,
        name: null,
        maxGroup: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加拼团审核";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      const id = row.id || this.ids
      getGroupa(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "拼团介绍："+response.data.name+"(ID "+response.data.id+")";
      });
    },

    // GroupId
    GroupId(row){
      this.centerDialogVisible=true
      this.dlog.remark=row.remark
      this.dlog.firstImg=row.firstImg
      this.dlog.secondImg=row.secondImg
      this.dlog.surfacePlot=row.surfacePlot
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            addGroupa(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    updatea(id){
      updateGroupa(id).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      });
    },
    updateab(id){
      updateGroupab(id).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除拼团审核编号为"' + ids + '"的数据项？').then(function() {
        return delGroupa(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/groupa/export', {
        ...this.queryParams
      }, `groupa_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
